package timing.ukulele.third.service.msg;

import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import timing.ukulele.data.third.data.msg.SendMessageDTO;
import timing.ukulele.third.msgChannel.action.send.SendAfterCheckAndProcessAction;
import timing.ukulele.third.msgChannel.action.send.SendAssembleAction;
import timing.ukulele.third.msgChannel.action.send.SendMessageDirectAction;
import timing.ukulele.third.msgChannel.action.send.SendPreCheckAndProcessAction;
import timing.ukulele.third.msgChannel.model.SendTaskInfo;
import timing.ukulele.third.msgChannel.mq.producer.MessageRabbitMqProducer;
import timing.ukulele.third.msgChannel.param.SendMessageParam;
import timing.ukulele.third.persistent.msg.MessageTemplate;

/**
 * 消息发放
 *
 * @author zh
 */
@Service
@AllArgsConstructor
public class SendMessageService {

    private final SendPreCheckAndProcessAction sendPreCheckAndProcessAction;

    private final SendAssembleAction sendAssembleAction;

    private final SendAfterCheckAndProcessAction sendAfterCheckAndProcessAction;

    private final SendMessageDirectAction sendMessageDirectAction;

    private final MessageRabbitMqProducer messageRabbitMqProducer;


    public Boolean sendMessageByBusiness(SendMessageDTO dto) {

        // 构造参数
        SendMessageParam sendMessageParam = SendMessageParam.builder()
                .messageTemplateId(dto.getMessageTemplateId())
                .receiver(dto.getReceiver())
                .variables(dto.getVariables())
                .extra(dto.getExtra()).build();

        // 消息发送前置检查
        MessageTemplate messageTemplate = sendPreCheckAndProcessAction.process(sendMessageParam);

        // 消息发送可变参数和默认值处理
        SendTaskInfo sendTaskInfo = sendAssembleAction.process(messageTemplate, sendMessageParam);

        // 后置参数处理
        sendAfterCheckAndProcessAction.process(sendTaskInfo);

        // 发送消息（先将消息推送至mq）
         sendMessageDirectAction.process(sendTaskInfo);
//        messageRabbitMqProducer.sendChannelMessageToMq(sendTaskInfo);

        return Boolean.TRUE;
    }

}
